﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using UnityEngine.UI;

public class FuncsBarLogic : LogicModuleBase<InputManager,SpriteManager>,ICombineSubUI
{

    GameObject target;
    List<MeshEntityData> currentEntity=new List<MeshEntityData>();
    MeshEntityData selected = null;
    List<GameObject> panels = new List<GameObject>();
    public void InitGameobject(List<GameObject> obj)
    {
        target = obj.Find(s => s.name == "FuncsBar");
        panels.Add(target.transform.Find("SkiScrollView/SkiViewport/SkiContent/Func").gameObject);
        panels[0].gameObject.SetActive(false);
        RefreshSkillsBar(-1);
    }
    public override void OnEnable()
    {
        base.OnEnable();
        target.SetActive(true);
        selected = null;
    }
    public override void OnDisable()
    {
        base.OnDisable();
        target.SetActive(false);
    }

    [RegisterEvent(CombineUIEvent.Receive_Data)]
    void Refresh(List<MeshEntityData> datas)//传入数据
    {
        currentEntity =new List<MeshEntityData>(datas);
        if (selected == null)
        {
            RefreshSkillsBar(-1);
        }
        else
        {
            RefreshSkillsBar(selected.ID);
        }

    }
    [RegisterEvent( CombineUIEvent.ShowObjectValues)]
    void ChangeSelectedItem(int id)
    {
        if (id <=0)
        {
            selected = null;
            RefreshSkillsBar(-1);
            return;
        }
        else
        {
            selected = currentEntity.Find(x => x.ID == id);
            RefreshSkillsBar(selected.ID);
        }
    }//选中物体改变时传入id，根据id找出对应道具

    void RefreshSkillsBar(int id = -1)
    {
        if (id == -1)//未选中物体时
        {
            if (panels.Count>0)
            {
                ClearPanel();
            }
            return;
        }
        else//选中物体了
        {
            if (panels.Count >0)
            {
                ClearPanel();
              /*  float x = target.transform.Find("SkiScrollView/SkiViewport/SkiContent").GetComponent<RectTransform>().rect.width;
                float y = target.transform.Find("SkiScrollView/SkiViewport/SkiContent/Func").GetComponent<RectTransform>().rect.height * panels.Count;
                target.transform.Find("SkiScrollView/SkiViewport/SkiContent").GetComponent<RectTransform>().sizeDelta = new Vector2(x, y);
                target.transform.Find("SkiScrollView/SkiViewport/SkiContent").transform.position = new Vector3(target.transform.Find("SkiScrollView/SkiViewport/SkiContent").transform.position.x, y,0);*/
            }
            var funcsinfo = selected.Funcs;
            if (funcsinfo.Count == 1)
            {
                target.transform.Find("SkiScrollView/SkiViewport/SkiContent/Func").gameObject.SetActive(true);
                RefreshPanel(panels[0], funcsinfo[0]);
            }
            else if(funcsinfo.Count>1)
            {
                target.transform.Find("SkiScrollView/SkiViewport/SkiContent/Func").gameObject.SetActive(true);
                RefreshPanel(panels[0], funcsinfo[0]);
                for (int i = 1; i < funcsinfo.Count; i++)
                {
                    panels.Add(GameObject.Instantiate(panels[0], panels[0].transform.parent));//复制加组
                    RefreshPanel(panels[i], funcsinfo[i]);
                }
            }
        }
    }
    void OnClickKeySettingButton(SFuncBaseVEx x)
    {
        Dispatch(CombineUIEvent.KeyChangedBefore,x);
    }
    void ClearPanel()
    {
        for (int i = 1; i < panels.Count; i++)
        {
            GameObject.Destroy(panels[i]);
            panels.RemoveAt(i);
        }
        target.transform.Find("SkiScrollView/SkiViewport/SkiContent/Func").gameObject.SetActive(false);
    }
    void RefreshPanel(GameObject Func,SFuncBaseVEx x)
    {
        Func.transform.Find("Image").GetComponent<Image>().sprite = SysModule1.GetSprite(x.FuncName);
        Func.transform.Find("SkillDescription/Text").GetComponent<Text>().text = x.FuncName + ":" + x.FuncInfo;
        Func.transform.Find("SkillDescription").position = panels[0].transform.Find("Image").transform.position;
        Func.transform.Find("Button").GetComponentInChildren<Text>().text = ((KeyCode)x.Key == KeyCode.None) ? "未设置按键" : ("按键: <color=#0028FF>" + ((KeyCode)x.Key).ToString() + "</color>");
        Func.transform.GetComponentInChildren<Button>().onClick.AddListener(() => { OnClickKeySettingButton(x); });
    }
}
